var router = require('express').Router();
var multer = require('multer');
var fs = require('fs');
var upload = multer({
  "dest": "upimgs"
});
var single = upload.single("image");


var currentAlbum = "default";
const albumMap = {
  "default": []
};
const albumNames = ["default"];


router.get("/", function(req,res){
  // res.locals.albumMap = albumMap;
  res.locals.albumNames = albumNames;
  // console.log(albumMap);
  res.locals.currentAlbum = albumMap[currentAlbum];
  console.log(currentAlbum);
  res.render("album");
});
router.get("/img/:filename", function(req,res){
  fs.createReadStream("upimgs/"+req.params.filename).pipe(res);
});
router.post("/upimg", single, function(req,res){
  albumMap[currentAlbum].push(req.file);
  res.send(req.file);
});
router.get("/images/:albumname", function(req,res){
  currentAlbum = req.params.albumname;
  res.send(albumMap[req.params.albumname]);
});
router.get("/getAlbum", function(req,res){
  console.log("getalbum=" ,albumNames);
  res.send(albumNames);
});
router.get("/createAlbum/:albumname", function(req,res){
  albumMap[req.params.albumname]=[];
  currentAlbum = req.params.albumname;
  albumNames.push(req.params.albumname);
  console.log(currentAlbum);
  console.log(albumNames);
  res.send(albumNames);
});

module.exports = router;
